package com.drug.controller;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/**
 * 登录验证拦截
 */

@Component
public class LoginInterceptor extends HandlerInterceptorAdapter {
    boolean aBoolean = true;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                             Object handler)throws Exception {

        HttpSession session = request.getSession();
        String username = (String) request.getSession().getAttribute("CHECKCODE_USERNAME");
        String  path =request.getServletPath();
        if (path.endsWith("footer.html")||path.endsWith("header.html")||path.endsWith("error")|| path.endsWith("login.html") || path.endsWith(".css")||path.endsWith(".js")||path.endsWith("png")||path.endsWith(".jpg")||path.endsWith("registerUser")||path.endsWith("findUsername")||path.endsWith("pdRole")||path.endsWith("logout")){
            return true;
        }
        if(null!=username) {//已登录

            return true;
        }else {//未登录
            //直接重定向到登录页面
                response.sendRedirect(request.getContextPath()+"/static/login.html");

            return false;
        }
    }

}

